package com.hzbank.swancs.utils;

import android.text.TextUtils;

import com.github.barteksc.pdfviewer.util.ThreadExecutorUtil;
import com.hzbank.swancs.view.menu.WxSwancsSystemMenuView;
import com.hzbank.swancs.view.menu.module.MenuOperatorModule;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
import com.taobao.weex.bridge.ModuleFactoryImpl;
import com.taobao.weex.bridge.WXModuleManager;
import com.taobao.weex.common.WXModule;
import com.taobao.weex.ui.IFComponentHolder;
import com.taobao.weex.ui.SimpleComponentHolder;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXComponentFactory;
import com.taobao.weex.ui.module.WXDomModule;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import mtopsdk.mtop.util.MtopSDKThreadPoolExecutorFactory;

/**
 * @类名: ReflectUtils
 * @描述: 反射工具类
 * @作者：fangkaijin
 * @邮箱: fangkaijin@outlook.com
 * @时间: 2022/7/22
 **/
public class ReflectUtils {

    public static Map<String, WXDomModule> getDomModuleMap(){

        try {

            Class clazz = Class.forName("com.taobao.weex.bridge.WXModuleManager");
            Constructor constructor = clazz.getDeclaredConstructor();

            constructor.setAccessible(true);

            Object instance = constructor.newInstance();

            Field field = clazz.getDeclaredField("sDomModuleMap");

            field.setAccessible(true);

            return (Map<String, WXDomModule>)field.get(instance);

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //展示通知弹框
    public static void showTopWindow(String content){

        try {

            if(TextUtils.isEmpty(content)){
                return;
            }

            Class clazz = Class.forName("com.hzbank.swancs.agoo.SwancsAgooService");
            Constructor constructor = clazz.getDeclaredConstructor();

            constructor.setAccessible(true);

            Object instance = constructor.newInstance();

            Method topWindow = clazz.getDeclaredMethod("addCustomViewOnTopWindow", String.class);

            topWindow.setAccessible(true);

            topWindow.invoke(instance, content);

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static boolean getRendered(WXSDKInstance instance){


        try {

            Class clazz = Class.forName("com.taobao.weex.WXSDKInstance");
            //Constructor constructor = clazz.getDeclaredConstructor();

            //constructor.setAccessible(true);

            Field field = clazz.getDeclaredField("mRendered");

            field.setAccessible(true);

            Boolean isRendered = (Boolean) field.get(instance);

            return isRendered.booleanValue();

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public static <T extends WXComponent> T getComponent(String instanceId, String ref){

        try {

            return (T)WXSDKManager.getInstance().getWXRenderManager().getWXComponent(instanceId, ref);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static <T extends WXModule> T getModule(String moduleName){

        try {

            ConcurrentMap<String, ModuleFactoryImpl> instanceMap = getInstanceModuleMap();

            if(null!=instanceMap){

                ModuleFactoryImpl moduleFactory = instanceMap.get(moduleName);

                if(null!=moduleFactory){

                    WXModule module = moduleFactory.mFactory.buildInstance();

                    if(null!=module ){

                        return (T)module;
                    }
                }
            }

            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    public static ConcurrentMap<String, ModuleFactoryImpl> getInstanceModuleMap(){

        try {

            Class clazz = Class.forName("com.taobao.weex.bridge.WXModuleManager");
            Constructor constructor = clazz.getDeclaredConstructor();

            constructor.setAccessible(true);

            Object instance = constructor.newInstance();

            Field field = clazz.getDeclaredField("sModuleFactoryMap");

            field.setAccessible(true);

            return (ConcurrentMap<String, ModuleFactoryImpl>)field.get(instance);

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    //Mtop 线程池 反射修改
    public static void modifyMTopThreadPool(){

        try{

            Field fieldB = MtopSDKThreadPoolExecutorFactory.class.getDeclaredField("b");
            fieldB.setAccessible(true);

            fieldB.set(null, ThreadExecutorUtil.mtopThread());

            Field fieldC = MtopSDKThreadPoolExecutorFactory.class.getDeclaredField("c");
            fieldC.setAccessible(true);

            fieldC.set(null, ThreadExecutorUtil.mtopThread());

            Field fieldD = MtopSDKThreadPoolExecutorFactory.class.getDeclaredField("d");
            fieldD.setAccessible(true);

            fieldD.set(null, ThreadExecutorUtil.mtopThreads());

        }catch (Exception e){
            e.printStackTrace();

            HLogger.logWithTag("ReflectUtils", "mtop线程池反射出错="+ ((null == e) ? "无异常信息" : e.getMessage()));
            //异常处理
            //ignore

        }
    }

}
